import moment from 'moment'
const Encrypt  = require('./jsencrypt.js');

//随机Key 
export function randomKey(number) {
	let key = ''
	let str = '0123456789abcdefghijklmnopqrstuvwxyz'
	for (let i = 0; i < number; i++) {
		let random = Math.random() * str.length
		let number = Math.floor(random)
		let ss = str.substr(number, 1)
		key += ss
	}
	return key
}

//RSA加密
export function getRSAEncrypt(publicKey, params) {
  let encrypt = new Encrypt.JSEncrypt()
	encrypt.setPublicKey(publicKey)
	return encrypt.encrypt(params)
}

// 日期格式化
export function dateFmtStr(dates, fmt = 'yyyy-MM-dd hh:mm:ss') {
	let reg;
	let date = new Date(dates)
	const opt = {
		"y+": date.getFullYear().toString(), // 年
		"M+": (date.getMonth() + 1).toString(), // 月
		"d+": date.getDate().toString(), // 日
		"h+": date.getHours().toString(), // 时
		"m+": date.getMinutes().toString(), // 分
		"s+": date.getSeconds().toString() // 秒
	};
	for (let k in opt) {
		reg = new RegExp("(" + k + ")").exec(fmt);
		if (reg) {
			fmt = fmt.replace(reg[1], (reg[1].length === 1) ? (opt[k]) : (opt[k].padStart(reg[1].length, "0")));
		}
	}
	return fmt;
}

// 到期时间计算
export function groupExpTime(times) {
	let nowDate = new Date().getTime(); //当前时间
	let day = 1000 * 60 * 60 * 24; //1天毫秒
	let fifteenTime = 15 * day; //15天毫秒
	let newDateTime = times - fifteenTime; //到期前15天
	let color;
	if (nowDate > times) {
		color = 'reds';
	} else {
		if (nowDate >= newDateTime) {
			color = 'orange';
		} else {
			color = 'primary';
		}
	}
	let changeTime = getLocalDate(times)
	return {
		time: changeTime,
		color
	}
}
// 设备是否购买
export function deviceIsBuy(times) {
	let nowDate = new Date().getTime(); //当前时间
	let day = 1000 * 60 * 60 * 24; //1天毫秒
	let fifteenTime = 15 * day; //15天毫秒
	let newDateTime = times - fifteenTime; //到期前15天
	let color;
	if (nowDate > times) {
		color = 'reds';
	} else {
		color = 'primary';
	}
	let changeTime = getLocalDate(times)
	return {
		time: changeTime,
		color
	}
}
// 设备类型图标
export function deviceTypeIcon(value){
	let icon;
	let type = Number(value);
	switch(type){
		case 20:
			icon='20';
			break;
		case 21:
		case 26:
		case 0x11130001:
			icon='21';
			break;
		case 22:
			icon='22';
			break;
		case 23:
			icon='23';
			break;
		case 0x11110027:
		case 0x11110031:
		case 0x11110033:
			icon='0x1'
			break;
		case 0x12310001:
			icon='0x2'
			break;
		case 0x12310002:
			icon = '0x3';
			break;
		case 0x12310003:
			icon = '24';
			break;
		default:{
			let standardNVR = 0x0F000000;
			let standardIOT = 0x0000FF00;
			let isNVR = (standardNVR & type) == 0x02000000 || (standardNVR & type) == 0x03000000;//判断是否是NVR
			if (isNVR && (standardIOT & type) > 0) {//判断是否是IOT
			    icon = '24';
			}else if (isNVR) {//判断是否是NVR
			    icon = '0x3';
			}else{
				icon = '0';
			}
			break;
		}
	}
	return icon
}

// 本月开始、结束时间
export function timeStartAndEnd(){
	const startDate = moment().month(moment().month()).startOf('month').valueOf();
	// const endDate = moment().month(moment().month()).endOf('month').valueOf();//当月末尾
	const endDate = moment().valueOf();
	return{
		startDate:getLocalDate(startDate,'YYYY-MM-DD'),
		endDate:getLocalDate(endDate,'YYYY-MM-DD'),
	}
}

// 获取本地时间
export function getLocalDate (time,format='YYYY-MM-DD HH:mm:ss') {
  const instance = new Date(new Date())
  const localOffset = instance.getTimezoneOffset() * 60000 // 获得当地时间偏移的毫秒数
  const utc = localOffset / (1000 * 60 * 60) // 时间差
  return moment(time).utcOffset(-utc).format(format)
}

// 获取最大值
export const handleMax = (datas,paramName)=>{
	let nums = [];
	for (var i = 0; i<datas.length; i++) {
		nums.push(datas[i][paramName]);
	}
	nums.sort(function(num1,num2){
		return num1-num2;
	})
	return nums[nums.length-1];
}

// 正则进行报错格式和特殊字符的替换
export function handleSpecialCharacters(jsonStr) {
  let obj = {};
  if (jsonStr && Object.prototype.toString.call(jsonStr) == "[object String]" && jsonStr != 'null') {
    jsonStr = jsonStr.replace(/\r/g, "\\r");
    jsonStr = jsonStr.replace(/\n/g, "\\n");
    jsonStr = jsonStr.replace(/\t/g, "\\t");
    jsonStr = jsonStr.replace(/\\/g, "\\\\");
    jsonStr = jsonStr.replace(/\'/g, "&#39;");
    jsonStr = jsonStr.replace(/ /g, "&nbsp;");
    jsonStr = jsonStr.replace(/</g, "$lt;");
    jsonStr = jsonStr.replace(/>/g, "$gt;");
    obj = JSON.parse(jsonStr)
  }
  return obj;
}

// base64编码、解码
export const Base64 = {
      _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
      encode: function (e) {
        var t = "";
        var n, r, i, s, o, u, a;
        var f = 0;
        e = Base64._utf8_encode(e);
        while (f < e.length) {
          n = e.charCodeAt(f++);
          r = e.charCodeAt(f++);
          i = e.charCodeAt(f++);
          s = n >> 2;
          o = (n & 3) << 4 | r >> 4;
          u = (r & 15) << 2 | i >> 6;
          a = i & 63;
          if (isNaN(r)) {
            u = a = 64
          } else if (isNaN(i)) {
            a = 64
          }
          t = t + this._keyStr.charAt(s) + this._keyStr.charAt(o) + this._keyStr.charAt(u) + this._keyStr.charAt(a)
        }
        return t
      },
      decode: function (e) {
        var t = "";
        var n, r, i;
        var s, o, u, a;
        var f = 0;
        e = e.replace(/[^A-Za-z0-9+/=]/g, "");
        while (f < e.length) {
          s = this._keyStr.indexOf(e.charAt(f++));
          o = this._keyStr.indexOf(e.charAt(f++));
          u = this._keyStr.indexOf(e.charAt(f++));
          a = this._keyStr.indexOf(e.charAt(f++));
          n = s << 2 | o >> 4;
          r = (o & 15) << 4 | u >> 2;
          i = (u & 3) << 6 | a;
          t = t + String.fromCharCode(n);
          if (u != 64) {
            t = t + String.fromCharCode(r)
          }
          if (a != 64) {
            t = t + String.fromCharCode(i)
          }
        }
        t = Base64._utf8_decode(t);
        return t
      },
      _utf8_encode: function (e) {
        e = e.replace(/rn/g, "n");
        var t = "";
        for (var n = 0; n < e.length; n++) {
          var r = e.charCodeAt(n);
          if (r < 128) {
            t += String.fromCharCode(r)
          } else if (r > 127 && r < 2048) {
            t += String.fromCharCode(r >> 6 | 192);
            t += String.fromCharCode(r & 63 | 128)
          } else {
            t += String.fromCharCode(r >> 12 | 224);
            t += String.fromCharCode(r >> 6 & 63 | 128);
            t += String.fromCharCode(r & 63 | 128)
          }
        }
        return t
      },
      _utf8_decode: function (e) {
        var t = "";
        var n = 0;
        var r = c1 = c2 = 0;
        while (n < e.length) {
          r = e.charCodeAt(n);
          if (r < 128) {
            t += String.fromCharCode(r);
            n++
          } else if (r > 191 && r < 224) {
            c2 = e.charCodeAt(n + 1);
            t += String.fromCharCode((r & 31) << 6 | c2 & 63);
            n += 2
          } else {
            c2 = e.charCodeAt(n + 1);
            c3 = e.charCodeAt(n + 2);
            t += String.fromCharCode((r & 15) << 12 | (c2 & 63) << 6 | c3 & 63);
            n += 3
          }
        }
        return t
      }
    }
	// 对象参数拼接到url上
export function objToParam(param) {
	let url='';
	if (Object.prototype.toString.call(param) !== '[object Object]') {
	    return url = '';
	}
	Object.keys(param).map((key)=>{
	    url += key + '=' + param[key] +'&';	
	})
	// 此处生成的 url 末尾多一个 & , .substring 截取一下
	url = url.substring(0, url.length - 1)
	return url;
}
// 门铃
export function doorbells(){
	return ['HADBL0002000100B','HADBL0016000100F','HADBL00170001007','HADBL0017001500J','HADBL1107034C00Z']
}
// 门锁
export function doorlock(){
	return ['HAVLS0008000100F','HAVLS00090001007','HAVLS00100001009','HAVLS0011000100A','HAVLS0012000100B','HAVLS0013000100C','HAVLS00140001008','HAVLS0015000100D','HAVLS0016000100E','HAVLS0017000100F','HAVLS00180001007','HAVLS198203D900K','HAVLS205403D900D','HAVLS137003D900D']
}
